package com.book.service.impl;

import com.book.entity.BookOrder;
import com.book.entity.BookOrderItem;
import com.book.mapper.BookCartMapper;
import com.book.mapper.BookOrderItemMapper;
import com.book.mapper.BookOrderMapper;
import com.book.service.OrderService;
import com.book.vo.OrderVo;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;

/**
 * @program:
 * @ClassName:
 * @description:
 * @author: zgc
 * @date:
 * @Version 1.0
 **/
@Service
public class OrderServiceImpl implements OrderService {
    @Resource
    private BookOrderMapper bookOrderMapper;
    
    @Resource
    private BookOrderItemMapper bookOrderItemMapper;
    
    @Resource
    private BookCartMapper bookCartMapper;
    
   
    
    @Override
    public int addOrder(BookOrder bookOrder, List<BookOrderItem> bookOrderItemList, int[] cartId) {
        //1、先增加订单
        bookOrderMapper.insertSelective(bookOrder);
        //2、新增订单的详情
        bookOrderItemMapper.insertOrderItems(bookOrderItemList);
        //3、再删除购物车相关的数据，因为信息下单完成，就要从数据库中删除
        bookCartMapper.deleteBookCartByCartId(cartId);
        
        return 0;
        
    }

    @Override
    public OrderVo findOrderByOrderId(long orderId) {
        return  bookOrderMapper.selectOrderByOrderId(orderId);
    }

    @Override
    public int changeAddressByOrderId(Long orderId, int addressId) {
        BookOrder order = new BookOrder();
        order.setId(orderId);
        order.setAddressId(addressId);
        return bookOrderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public int updateOrderOrderStatus(BookOrder bookOrder) {
        BookOrder order = new BookOrder();
        order.setTradeNo(bookOrder.getTradeNo());
        order.setStatus(bookOrder.getStatus());
        order.setUpdateTime(new Date());
        return bookOrderMapper.updateByPrimaryKeySelective(order);
    }

    @Override
    public List<BookOrder> showAllPayOrder(Long userId) {
        Example example = new Example(BookOrder.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("userId",userId);
        c.andIsNotNull("tradeNo");
        c.andEqualTo("status",1);
        return bookOrderMapper.selectByExample(example);
    }

    @Override
    public List<BookOrderItem> showDetailOrder(Long orderId) {
        Example example = new Example(BookOrderItem.class);
        Example.Criteria c = example.createCriteria();
        c.andEqualTo("orderId",orderId);
        return bookOrderItemMapper.selectByExample(example);
    }


    @Override
    public List<BookOrder> waitPayOrder(Long userId) {
        Example example = new Example(BookOrder.class);
        Example.Criteria c = example.createCriteria();
        
        c.andEqualTo("userId",userId);
        c.andEqualTo("status",0);
        
        return bookOrderMapper.selectByExample(example);
    }

    @Override
    public int updatePayStatus(BookOrder bookOrder) {
        return bookOrderMapper.updateByPrimaryKeySelective(bookOrder);
    }

    @Override
    public List<BookOrder> selectWaitOrder(Long userId, Byte status) {
        Example example = new Example(BookOrder.class);
        Example.Criteria c = example.createCriteria();
        
        c.andEqualTo("userId",userId);
        c.andEqualTo("status",status);
        return bookOrderMapper.selectByExample(example);
    }
}

